package com.lgx.mobile.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.dao.ITopicChatRecordDao;
import com.lgx.mobile.entity.TopicChatRecord;
import com.lgx.mobile.vo.TopicChatRecordVO;

/**
 * 
 * @author xuyichen
 *
 */
@Component("topicChatRecordDao")
public class TopicChatRecordDao extends GeneralDao implements ITopicChatRecordDao {
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	protected Logger log = Logger.getLogger(this.getClass());
	
	public TopicChatRecord createTopicChatRecord(TopicChatRecord topicChatRecord) throws DataException {
		this.getHibernateTemplate().save(topicChatRecord);
		log.info("[创建话题聊天记录]:"+topicChatRecord.getId());
		return topicChatRecord;
	}

	public TopicChatRecord updateTopicChatRecord(TopicChatRecord topicChatRecord) throws DataException {
		this.getHibernateTemplate().update(topicChatRecord);
		log.info("[更新话题聊天记录]:"+topicChatRecord.getId());
		return topicChatRecord;
	}

	public void deleteTopicChatRecord(TopicChatRecord topicChatRecord) throws DataException {
		log.info("[删除话题聊天记录]:"+topicChatRecord.getId());
		this.getHibernateTemplate().delete(topicChatRecord);
	}

	public void deleteTopicChatRecordById(long id) throws DataException {
		log.info("[删除话题聊天记录]:"+id);
		this.deleteTopicChatRecord(this.findTopicChatRecordById(id));
	}

	public TopicChatRecord findTopicChatRecordById(long id) {
		return this.getHibernateTemplate().get(TopicChatRecord.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<TopicChatRecord> findTopicChatRecordListByTopicId(long topicId){
		String sql = "from TopicChatRecord tcr where tcr.topicId = "+topicId+" order by tcr.id asc";
		return this.getHibernateTemplate().find(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<TopicChatRecord> findTopicChatRecordList(){
		String sql = "from TopicChatRecord tcr order by tcr.id asc";
		return this.getHibernateTemplate().find(sql);
	}

	@Override
	public Pagination findTopicChatRecordPaginationByTopicId(long topicId, Object[] params, int pageSize, int pageNum) {
		String sql = "select tcr.id as id," +
				"tcr.record_date as recordDate," +
				"tcr.content as content," +
				"tcr.topic_id as topicId," +
				"tcr.user_id as userId," +
				"u.nickname as nickName," +
				"u.username as userName," +
				"u.truename as trueName";

		sql += " from tm_m_topic_chat_record tcr left join ts_user u on u.id = tcr.user_id where u.isdelete = '0' ";
		
		sql += " and tcr.topic_id = " + topicId;
		
		sql += " order by tcr.record_date desc";
		
		return baseDao.queryJdbcListPagination(sql, TopicChatRecordVO.class, pageSize, pageNum);
	}
}
